package com.ht.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.modules.system.entity.SysDepart;
import com.ht.modules.system.entity.SysUserDepart;
import com.ht.modules.system.entity.SysUserVo;
import com.ht.modules.system.mapper.SysUserDepartMapper;
import com.ht.modules.system.model.DepartIdModel;
import com.ht.modules.system.model.SysUserDepartsVO;
import com.ht.modules.system.service.ISysDepartService;
import com.ht.modules.system.service.ISysUserDepartService;
import com.ht.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 用户部门表实现类
 */
@Service
public class SysUserDepartServiceImpl extends ServiceImpl<SysUserDepartMapper, SysUserDepart> implements ISysUserDepartService {

	@Autowired
	private ISysDepartService sysDepartService;
	@Autowired
	private ISysUserService sysUserService;
    @Autowired
    private SysUserDepartMapper sysUserDepartMapper;

	/**
	 *根据用户id添加部门信息
	 */
	@Override
	public boolean addSysUseWithrDepart(SysUserDepartsVO sysUserDepartsVO) {
		LambdaQueryWrapper<SysUserDepart> query = new LambdaQueryWrapper<>();
		if(sysUserDepartsVO != null) {
			String userId = sysUserDepartsVO.getUserId();
			List<String> departIdList = sysUserDepartsVO.getDepartIdList();
			if(departIdList != null && !departIdList.isEmpty()) {
				for(String depId : departIdList) {
					query.eq(SysUserDepart::getDepId, depId);
					query.eq(SysUserDepart::getUserId, userId);
					List<SysUserDepart> uDepList = this.list(query);
					if(uDepList == null || uDepList.isEmpty()) {
						this.save(new SysUserDepart("",userId,depId));
					}
				}
			}
			return true;
		}else {
			return false;
		}
	}

	/**
	 * 根据用户id查询部门信息
	 */
	@Override
	public List<DepartIdModel> queryDepartIdsOfUser(String userId) {
		LambdaQueryWrapper<SysUserDepart> queryUDep = new LambdaQueryWrapper<>();
		LambdaQueryWrapper<SysDepart> queryDep = new LambdaQueryWrapper<>();
		try {
			queryUDep.eq(SysUserDepart::getUserId, userId);
			List<String> depIdList = new ArrayList<>();
			List<DepartIdModel> depIdModelList = new ArrayList<>();
			List<SysUserDepart> userDepList = this.list(queryUDep);
			if(userDepList != null && !userDepList.isEmpty()) {
			for(SysUserDepart userDepart : userDepList) {
					depIdList.add(userDepart.getDepId());
				}
			queryDep.in(SysDepart::getId, depIdList);
			List<SysDepart> depList = sysDepartService.list(queryDep);
			if(depList != null && !depList.isEmpty()) {
				for(SysDepart depart : depList) {
					depIdModelList.add(new DepartIdModel().convertByUserDepart(depart));
				}
			}
			return depIdModelList;
			}
		}catch(Exception e) {
			e.fillInStackTrace();
		}
		return null;
	}

	/**
	 * 根据用户id修改部门信息
	 */
	@Override
	public boolean editSysUserWithDepart(SysUserDepartsVO sysUserDepartsVO) {
		LambdaQueryWrapper<SysUserDepart> queryDep = new LambdaQueryWrapper<>();
		List<String> depIdList = sysUserDepartsVO.getDepartIdList();
		if(depIdList != null && !depIdList.isEmpty()) {
			queryDep.eq(SysUserDepart::getUserId, sysUserDepartsVO.getUserId());
			boolean ok = this.remove(queryDep);
			if(ok) {
				for(String str : depIdList) {
					this.save(new SysUserDepart("", sysUserDepartsVO.getUserId(), str));
				}
			return ok;
			}
		}
		queryDep.eq(SysUserDepart::getUserId, sysUserDepartsVO.getUserId());
		return this.remove(queryDep);
	}

	/**
	 * 根据部门id查询用户信息
     *
     * 原接口无法根据用户是否冻结过滤
     * 因为之前写过相同接口，为了不改变前端与手机端代码
     * 并且/sys/user/queryUserVoByDepId这个接口没有被前端调用
     * 所以此处与queryUserVoByDepId接口内容相同
     * 如果发现问题，则直接回滚至上一版，并且更改前端调用接口即可
	 */
	@Override
	public List<SysUserVo> queryUserByDepId(String depId, String username, String realname) {
		return sysUserService.queryUserVoByDepId(depId, username, realname);
	}

	@Override
	public List<SysUserVo> queryUserVoByDepId(String depId, String username, String realname) {
		return sysUserService.queryUserVoByDepId(depId, username, realname);
	}

    @Override
    public boolean isBelongToProductDept(String userId) {
        return sysUserDepartMapper.isBelongToProductDept(userId) > 0;
    }

	@Override
	public Set<String> getUserIdSet(String departId) {
		return sysUserDepartMapper.getUserIdSet(departId);
	}
}
